import {
  BadRequestException,
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Patch,
  Post,
  Put,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { OrderService } from '@/modules/order/order.service';
import { JwtAuthGuard } from '@/auth/guard/jwt-auth.guard';
import { OrderEntity } from '@/entity/order.entity';
import { CreateOrderDto } from '@/modules/order/dto/create-order.dto';
import { UpdateOrderDto } from '@/modules/order/dto/update-order.dto';
import { Roles, RolesGuard } from '@/auth/guard/roles.guard';
import { CurrentUser } from '@/decorators/current-user.decorator';

@ApiTags('订单模块')
@Controller('order')
@UseGuards(JwtAuthGuard, RolesGuard)
export class OrderController {
  constructor(private readonly orderService: OrderService) {}

  @ApiResponse({
    status: 200,
    description: '成功查询所有订单',
    type: OrderEntity,
  })
  @ApiOperation({ summary: '查询所有订单' })
  @ApiBearerAuth()
  @Roles('admin')
  @Get()
  async findAll(@Query('page') page = 1, @Query('limit') limit = 10) {
    if (isNaN(Number(page)) || isNaN(Number(limit))) {
      throw new BadRequestException('page or limit must be number');
    }
    if (page < 1) page = 1;
    if (limit < 1) limit = 10;
    return this.orderService.findAll(page, limit);
  }

  @ApiOperation({ summary: '根据ID查询订单' })
  @ApiBearerAuth()
  @Get('order-detail/:id')
  async findOne(@Param('id') id: number): Promise<OrderEntity> {
    return this.orderService.findOne(id);
  }

  @ApiOperation({ summary: '查询当前用户的订单列表' })
  @Get('user')
  findByUserId(@CurrentUser() currentUser: any) {
    const userId = currentUser.user_id;
    return this.orderService.findByUserId(userId);
  }

  @ApiOperation({ summary: '查看当前用户不同状态的订单' })
  @ApiParam({
    name: 'status',
    description: '订单状态：0-未支付，1-已支付，2-正在制作，3-已上菜',
  })
  @Get('user/status/:status')
  findByUserAndStatus(
    @CurrentUser() currentUser: any,
    @Param('status') status: string,
  ) {
    const userId = currentUser.user_id;
    return this.orderService.findByUserAndStatus(userId, status);
  }

  @ApiOperation({ summary: '创建订单' })
  @ApiResponse({ status: 201, description: '成功创建订单', type: OrderEntity })
  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Post()
  async create(
    @Body() createOrderDto: CreateOrderDto,
    @CurrentUser() user,
  ): Promise<OrderEntity> {
    createOrderDto.userId = Number(user.user_id);
    const dishList = JSON.parse(createOrderDto.dishList);
    if (!Array.isArray(dishList)) {
      throw new BadRequestException('dishList必须是数组');
    }
    return this.orderService.create(createOrderDto);
  }

  @ApiOperation({ summary: '根据ID删除订单' })
  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Delete(':id')
  async delete(@Param('id') id: number): Promise<void> {
    return this.orderService.delete(id);
  }

  @ApiOperation({ summary: '根据ID更新订单' })
  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard)
  @Put(':id')
  async update(
    @Param('id') id: number,
    @Body() updateOrderDto: UpdateOrderDto,
  ): Promise<OrderEntity> {
    return this.orderService.update(id, updateOrderDto);
  }
}
